#include "StdAfx.h"
#include "PDAfederate.h"
#include "LRCambassador.h"
#include "LRCambassador.h"

using namespace rti1516;
using namespace std;

PDAfederate::PDAfederate(void)
{
}

PDAfederate::~PDAfederate(void)
{
}

void PDAfederate::start(void)
{
	LRCambassador amb;
	PDAfederateAmbassador famb;

	amb.createFederationExecution(L"pda federate execution");
	amb.joinFederationExecution(L"pdaRTIsimple1516", L"pdaRTIsimple1516", famb);

	while(true)
	{
//		amb.updateAttributeValues();
		amb.evokeCallback();

		Sleep(2000);
	}

	amb.resignFederationExecution();
	amb.destroyFederationExecution(L"pdaRTIsimple1516");
}



#include <stdio.h>
#include <wchar.h>
#include <sstream>
#include <string>
#include <iostream>
#include <sstream>
#include "stringUtil.h"



// Data shared between federate and federate ambassador
//DtTalkAmbData theAmbData;

// The object class name
std::wstring theClassName = L"BaseEntity";

// The interaction class name
std::wstring theInterClassName = L"WeaponFire";


////////////////////////////////////////////////////////////////////////////////
// Create the federation execution
void createFedEx(LRCambassador & rtiAmb,
				 std::wstring const& fedName)
{
	wcout << L"createFederationExecution "
		<< fedName.c_str() << endl;

	rtiAmb.createFederationExecution(fedName);


	wcout << L"Federation Created." << endl;
}

////////////////////////////////////////////////////////////////////////////////
// Join the federation execution
void joinFedEx(
			   LRCambassador & rtiAmb, rti1516::FederateAmbassador & fedAmb,
			   std::wstring const& federateType, std::wstring const& federationName)
{
	bool joined=false;
	const int maxTry  = 10;
	int numTries = 0;
	wcout << L"joinFederationExecution "
		<< federateType.c_str() << L" "
		<< federationName.c_str() << endl;

	while (!joined && numTries++ < maxTry)
	{
		//         rtiAmb.joinFederationExecution(federateType, federationName, fedAmb);
		joined = true;
	}
	if (joined)
		wcout << L"Joined Federation." << endl;
	else
	{
		wcout << L"Giving up." << endl;
		rtiAmb.destroyFederationExecution(federationName);
		exit(0);
	}
}

////////////////////////////////////////////////////////////////////////////////
// Resign and destroy the federation execution
void resignAndDestroy( LRCambassador & rtiAmb,
					  std::wstring const& federationName)
{
	rtiAmb.resignFederationExecution();
	rtiAmb.destroyFederationExecution(federationName);
}

////////////////////////////////////////////////////////////////////////////////
// Publish and subscribe the object class attributes.
// Register an object instance of the class.
bool publishSubscribeAndRegisterObject(LRCambassador & rtiAmb)
{
	return false;
}

////////////////////////////////////////////////////////////////////////////////
// Publish and Subscribe to an interaction class
bool publishAndSubscribeInteraction(LRCambassador & rtiAmb)
{
	return false;
}


int main2(int argc, char** argv)
{
	std::wstring federationName(L"MAKsimple");
	std::wstring federationFile(L"MAKsimple.xml");
	std::wstring federateType(L"rtisimple1516");

	//    RTI and Federate Ambassadors
	//    RTIambassadorFactory* rtiAmbFactory = new RTIambassadorFactory();
	//     std::auto_ptr < LRCambassador > rtiAmbAP = NULL;
	//	  rtiAmbFactory->createRTIambassador(args);
	LRCambassador* rtiAmb = new LRCambassador;
	rti1516::PDAfederateAmbassador fedAmb;
	// Create the federation
	createFedEx(*rtiAmb, federationName);

	// Join the federation
	joinFedEx(*rtiAmb, fedAmb, federateType, federationName);

	// Publish, subscribe and register and object
	if (!publishSubscribeAndRegisterObject(*rtiAmb))
	{
		resignAndDestroy(*rtiAmb, federationName);
		return 0;
	}

	// Publish and subscribe to the required interaction
	if (!publishAndSubscribeInteraction(*rtiAmb))
	{
		resignAndDestroy(*rtiAmb, federationName);
		return 0;
	}

	long count=0;
	while (1)
	{
		std::stringstream ss;
		ss << "1516-" << count++;
		std::string tag(ss.str());
		// Tag format is narrow string representation compatible
		// with 1.3 simple federate

		// Update the object
		rtiAmb->updateAttributeValues();  

		rtiAmb->evokeCallback();
	}

	// Resign and destroy federation
	resignAndDestroy(*rtiAmb, federationName);

	return 0;

}


